package com.example.springboot_jdk8.service.impl;

import cn.hutool.json.JSONArray;
import com.example.springboot_jdk8.entity.MdmOrg;
import com.example.springboot_jdk8.enums.PullDataConfigEnum;
import com.example.springboot_jdk8.service.AbsPullOrgData;
import com.example.springboot_jdk8.service.IConvertData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.example.springboot_jdk8.enums.PullDataConfigEnum.FULL;
import static com.example.springboot_jdk8.enums.PullDataConfigEnum.ORG;

/**
 * 全量拉取组织数据 + 全量转换组织数据
 */
@Slf4j
@Service
public class FullPullOrgDataImpl extends AbsPullOrgData<MdmOrg> {

    private final PullDataConfigEnum FINAL_FULL = FULL;

    private final PullDataConfigEnum FINAL_ORG = ORG;

    /**
     * 注入转换策略类
     */
    @Autowired
    private List<IConvertData<MdmOrg>> convertData;

    /**
     * 拉取组织数据
     * @param pullDataConfigEnum
     * @return
     */
    @Override
    public boolean supportPullDataType(PullDataConfigEnum pullDataConfigEnum){
        return pullDataConfigEnum.equals(FINAL_ORG);
    }

    /**
     * 拉取模式: 默认全量拉取
     * @param pullDataConfigEnum
     * @return
     */
    @Override
    public boolean supportPullDataModel(PullDataConfigEnum pullDataConfigEnum) {
        return pullDataConfigEnum.equals(FINAL_FULL);
    }


    /**
     * 全量拉取组织数据 + 全量数据转换
     * @return
     */
    @Override
    public List<MdmOrg> pullData() {
        log.info("FullPullOrgDataImpl pullData ......");
//        JSONArray orgs = MdmRequestUtil.getOrgs(Collections.emptyMap());
        JSONArray objects = new JSONArray();
        objects.add(11);
        objects.add(22);
        //过滤出能够支持转换当前数据的 转换器 (全量组织转换器)
        IConvertData iConvertData = convertData.stream()
                .filter(curIConvertData -> curIConvertData.supportConvertDataType(FINAL_ORG) && curIConvertData.supportConvertDataModel(FINAL_FULL))
                .findFirst().orElse(null);
        //执行转换操作
        return iConvertData.convert(objects);
    }
}
